package com.shijie.controller.common;

import com.shijie.component.tencent.TencentCloudCosClient;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by DELL on 2018/8/14.
 */


/**
 * 公共操作
 */
@RestController
@RequestMapping(value = "/common")
public class CommonController {

    @Resource
    private TencentCloudCosClient tencentCloudCosClient;

    /**
     * 上传图片
     */
    @PostMapping(value = "/uploadImage")
    public Result uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            String url = tencentCloudCosClient.uploadFile2Cos(file);
            Map<String, String> map = new HashMap<String, String>();
            System.out.println(file.getContentType());
            map.put("url", url);
            map.put("fileType", file.getContentType().substring(0,file.getContentType().indexOf("/")));
            map.put("fileName", file.getOriginalFilename());
            return ResultGenerator.success(map);
        } catch (Exception e) {
            return ResultGenerator.error("系统异常！");
        }


    }

}



